# frozen_string_literal: true
# == Schema Information
#
# Table name: editors
#
#  id              :integer          not null, primary key
#  village_id      :integer
#  name            :string
#  telephone       :string
#  password_digest :string
#  token           :string
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  registration_id :string
#
# Indexes
#
#  index_editors_on_village_id  (village_id)
#

class Editor < ApplicationRecord

  rolify

  has_secure_password

  has_secure_token

  before_validation :init_password, on: :create

  def init_password
    self.password  = '123456'
  end

  # optional: true  为了系统管理员
  belongs_to :village, optional: true
  has_many :buildings

  def system?
    has_role? 'system'
  end

  def estate?
    has_role? 'estate'
  end

  def floor?
    has_role? 'floor'
  end

  def door?
    has_role? 'door'
  end

  def current_role
    if system?
      '系统管理员'
    elsif estate?
      '小区管理员'
    elsif floor?
      '楼栋管理员'
    elsif door?
      '门卫'
    else
      '未知'
    end
  end
end
